package com.huitong.common.core.validator;

import com.huitong.common.core.annotation.Logical;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.List;

/**
 * 逻辑标识字段参数校验器
 *
 * @author fromdrowning
 * @date 2024/5/10 17:56
 */
public class LogicalValidator implements ConstraintValidator<Logical, Integer> {

    /**
     * 标识集合
     */
    private List<Integer> logicalTypeList;

    /**
     * 初始化 获取到注解上的参数值
     *
     * @param constraintAnnotation id字段参数校验注解
     */
    @Override
    public void initialize(Logical constraintAnnotation) {
        this.logicalTypeList = constraintAnnotation.type().getLogicalTypeList();
    }

    /**
     * 参数校验
     *
     * @param value                      逻辑标识字段的值
     * @param constraintValidatorContext 校验器上下文
     * @return {@code true} 校验成功
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        return logicalTypeList.contains(value);
    }
}
